package edu.humc.service.Impl;

import edu.humc.dao.UserMapper;
import edu.humc.pojo.User;
import edu.humc.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service("UserServiceImpl")
public class UserServiceImpl implements UserService {
    @Resource(name = "UserMapper")
    UserMapper userMapper;
    @Override
    public boolean UserLogin(User user) {
        User user1=userMapper.SelectUser(user);
        if (user1==null){
            return false;
        }
        if (user1.getPassword().equals(user.getPassword())) {
            return true;
        }
        return false;
    }
//    查询用户id

    @Override
    public int SelectUserId(User user) {
        return userMapper.SelectUserID(user);
    }

    @Override
    public boolean UserRegister(User user) {
        int result=userMapper.InsertUser(user);
        if (result>0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean UpdateUser(User user,String newpassword) {
        //利用登录逻辑判断用户是否存在
        if (UserLogin(user)) {
//            如果用户存在，将id写入传过来的user对象
            user.setId(userMapper.SelectUser(user).getId());
//            将传入的用户password字段修改为修改为新密码
            user.setPassword(newpassword);
//            判断密码是否修改成功
            if (userMapper.UpdateUser(user) > 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean DeleteUser(int id) {
//        判断是否注销成功
        if (userMapper.DeleteUser(id)>0) {
            return true;
        }
        return false;
    }
//    查找用户所有信息

    @Override
    public User SelectUserByid(int id) {
        return userMapper.SelectUserByid(id);
    }
    //更新用户信息

    @Override
    public boolean UpdatePersonMessage(User user) {
        if (userMapper.UpdatePersonMessage(user) > 0) {
            return true;
        }
        return false;
    }
}
